﻿using BussinesLayer.Services;
using DataAccess.Datatypes;
using Microsoft.AspNet.SignalR;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebMatrix.WebData;

namespace Mvc4.Hubs
{
    public class GoodsHub : Hub
    {
        private GoodServices gs = new GoodServices();

        public void AddContent(int goodId, string url, string comment)
        {
            int userId = WebSecurity.CurrentUserId;
            if (url == null)
            {
                url = string.Empty;
            }
            gs.AddContent(goodId, url, comment, DateTime.Now, userId);

            Clients.Caller.ConfirmAddContent();
        }

        public void GetNotAvailableDays(int goodId, int monthAux, int year)
        {
            int month = monthAux + 1;
            IEnumerable<NotAvailable> notAvList = gs.GetGoodAvailability(goodId);
            if (notAvList != null)
            {
                List<DateTime> notAvDays = new List<DateTime>();
                DateTime beginMonth = new DateTime(year, month, 1);
                DateTime endMonth = new DateTime(year, month, DateTime.DaysInMonth(year, month));
                foreach (NotAvailable elem in notAvList)
                {
                    DateTime intervalStart = beginMonth;
                    DateTime intervalEnd = endMonth;
                    if (DateTime.Compare(elem.Start, beginMonth) > 0)
                    {
                        intervalStart = elem.Start;
                    }
                    if (DateTime.Compare(elem.Finish, endMonth) < 0)
                    {
                        intervalEnd = elem.Finish;
                    }

                    while(DateTime.Compare(intervalStart, intervalEnd) <= 0)
                    {
                        notAvDays.Add(intervalStart);
                        intervalStart = intervalStart.AddDays(1);
                    }
                }
                string[][] notAvDaysMatrix = new string[notAvDays.Count()][];
                int i = 0;
                foreach(DateTime date in notAvDays)
                {
                    notAvDaysMatrix[i] = new string[4];
                    notAvDaysMatrix[i][0] = date.Day + "/" + date.Month + "/" + date.Year;
                    notAvDaysMatrix[i][1] = "";
                    notAvDaysMatrix[i][2] = "";
                    notAvDaysMatrix[i][3] = "red";
                    i++;
                }
                Clients.Caller.SetNotAvailableDays(notAvDaysMatrix, monthAux, year);
            }
        }

        public void AddLike(int goodId, int userId)
        {
            gs.AddLike(goodId, userId);
        }

        public bool RemoveLike(int goodId, int userId)
        {
            gs.RemoveLike(goodId, userId);

            return true;
        }

        public void AddInnapropiate(int contentId, int userId)
        {
            gs.AddInnapropiate(contentId, userId);
        }
    }
}